package dth.bean;

import java.sql.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.swing.text.StyledEditorKit.BoldAction;

import org.hibernate.Query;
import org.primefaces.event.FlowEvent;

import dth.dao.BookMovieDAO;
import dth.dao.MovieDAO;
import dth.dao.PaymentHistoryDAO;
import dth.dao.UserDAO;
import dth.entity.Account;
import dth.entity.BookMovie;
import dth.entity.PaymentHistory;
import dth.entity.User;
import dth.utils.DthUtil;

@ManagedBean(name = "rechargeBean")
public class RechargeBean {
	
	private int cusID;
	private int cusIDT;
	
	public int getCusIDT() {
		return cusIDT;
	}

	public void setCusIDT(int cusIDT) {
		this.cusIDT = cusIDT;
	}

	private String cardNum;
	private int numPayMonth;
	private double priceMovie;
	private double pricePackage;
	private String priceMovieStr;
	private String pricePackageStr;
	private Boolean searchID;
	private Boolean button;
	private String message;
	private Boolean cmessage;
	private int numOM;
	private int numOMT;
	private double total;
	private String totalStr;

	public String getTotalStr() {
		return totalStr;
	}

	public void setTotalStr(String totalStr) {
		this.totalStr = totalStr;
	}

	public int getNumOMT() {
		return numOMT;
	}

	public void setNumOMT(int numOMT) {
		this.numOMT = numOMT;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public String getPriceMovieStr() {
		return priceMovieStr;
	}

	public void setPriceMovieStr(String priceMovieStr) {
		this.priceMovieStr = priceMovieStr;
	}

	public String getPricePackageStr() {
		return pricePackageStr;
	}

	public void setPricePackageStr(String pricePackageStr) {
		this.pricePackageStr = pricePackageStr;
	}

	public int getNumOM() {
		return numOM;
	}

	public void setNumOM(int numOM) {
		this.numOM = numOM;
	}

	public Boolean getCmessage() {
		return cmessage;
	}

	public void setCmessage(Boolean cmessage) {
		this.cmessage = cmessage;
	}

	public void setPricePackage(double pricePackage) {
		this.pricePackage = pricePackage;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Boolean getButton() {
		return button;
	}

	public void setButton(Boolean button) {
		this.button = button;
	}

	public Boolean getSearchID() {
		return searchID;
	}

	public void setSearchID(Boolean searchID) {
		this.searchID = searchID;
	}

	public int getCusID() {
		Account ac = DthUtil.getAccountDTH();
		if (ac != null) {
			return ac.getFkUserId();
		}
		return cusID;
	}

	public void setCusID(int cusID) {
		this.cusID = cusID;
	}

	public String getCardNum() {
		return cardNum;
	}

	public void setCardNum(String cardNum) {
		this.cardNum = cardNum;
	}

	public int getNumPayMonth() {
		return numPayMonth;
	}

	public void setNumPayMonth(int numPayMonth) {
		this.numPayMonth = numPayMonth;
	}

	public double getPriceMovie() {
		return priceMovie;
	}

	public void setPriceMovie(double priceMovie) {
		this.priceMovie = priceMovie;
	}

	public double getPricePackage() {
		return pricePackage;
	}

	public void setPricePackage(int pricePackage) {
		this.pricePackage = pricePackage;
	}

	public void onFlowProcess(FlowEvent event) {

	}

	public void search() // tim customer voi so tien phai tra
	{
		System.out.println("cusId = " + cusID + " Card munber= " + cardNum);
		cusIDT =cusID;
		if (cusID == 0) {
			DthUtil.showMessage("Error", "Bạn chưa điền đủ thông tin.");
		} else {
			UserDAO userdao = new UserDAO();
			User user = userdao.checkUserByCusID(cusID);

			if (user == null) {
				message = "ID sai";
				cmessage = true;
			} else {
				cardNum = user.getCardNumber();
				BookMovieDAO bookDao = new BookMovieDAO();
				MovieDAO movieDao = new MovieDAO();
				List<BookMovie> lst = bookDao.getListBookMovie(cusID);
				int sumBM = 0;
				for (BookMovie bookMovie : lst) {
					sumBM += bookMovie.getOwnMovie().getMoviePrice();
				}
				numOM = user.getMonthsRecharge(); // số tháng mà user chọn thanh
													// toán ( 1 tháng, 2 tháng
													// ... )
				numOMT = (userdao.getNumMonthNoCharge(cusID) / numOM) * numOM;
				// numOMT = userdao.getNumMonthNoCharge(cusID); // số tháng mà
				// user phải trả nợ
				priceMovie = sumBM;
				priceMovieStr = DthUtil.getFormatedNumber(priceMovie);
				pricePackage = userdao.getPriceChargePerMonth(cusID);
				pricePackageStr = DthUtil.getFormatedNumber(pricePackage);
				total = numOMT * pricePackage + priceMovie;
				totalStr = DthUtil.getFormatedNumber(total);
				if (total > 0) {
					searchID = true;
					button = false;
					cmessage = false;
				}
				else {
					DthUtil.showMessage("Hi", "Tiền thanh toán của bạn là 0$");
				}
			}
		}
	}
	
	public void updateStatusMovie(int id){
		BookMovieDAO bookDao = new BookMovieDAO();
		bookDao.updateStatus(id);
	}
	public void inserPay(PaymentHistory pay)
	{
		PaymentHistoryDAO payHis = new PaymentHistoryDAO();
		int count = payHis.insert(pay);
		if(count == 1){
			DthUtil.showMessage("Thanks", "Thanh toán thành công!");
		}
		else{
			DthUtil.showMessage("Okay", "Something is wrong!");
		}
	}
	public void Recharge() {
		System.out.println("Recharge");
		System.out.println("Total: " + total + "CusID: " + cusID +" total: "+ total);
				
		PaymentHistory pay = new PaymentHistory();
		java.util.Date date = new java.util.Date();
		pay.setDateInsert(new Date(date.getTime()));
		pay.setFkPaymentUser(cusID);
		pay.setPrice(total);
		
		updateStatusMovie(cusID);
		inserPay(pay);
		searchID = false;
		button = true;
	}
	
	
	
	public RechargeBean() {
	
		System.out.println("--RechargeBean");
		Object objectAction=DthUtil.getRequest().getParameter("action");
		String ob = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:"+objectAction);
		if(objectAction!=null)
		{
			if(objectAction.equals("recharge"))
			{
				String card = DthUtil.getRequest().getParameter("cardNum");
				String cusid = DthUtil.getRequest().getParameter("cusID");
				String total = DthUtil.getRequest().getParameter("total");
				System.out.println("action recharge: cardNum = "+ card+" CusID = "+ cusid+ " total: " +total);
				totalStr = DthUtil.getFormatedNumber(Double.parseDouble(total));
				cusID = Integer.parseInt(cusid);
				cardNum = card;
				this.total = Double.parseDouble(total);
			}
		}
	}
}
